<template>
  <div class="account-settings-info-view">
    <a-row :gutter="16" type="flex" justify="center">
      <a-col :order="isMobile ? 2 : 1" :md="24" :lg="16">

        <a-form :form="form" layout="vertical">
          <!-- <a-form-item
            label="登录账号"
          >
            <a-input
              v-decorator="[
                'LoginName',{
                  rules: [
                    { required: true, message: '', whitespace: true, trigger: 'change' },
                    { validator: validateLoginName }
                  ]
                }
              ]"
              placeholder="请输入您的登录账号"
              @change="handleChangeLoginName"
            >
            </a-input>
          </a-form-item> -->
          <a-form-item
            label="姓名"
          >
            <a-input
              v-decorator="[
                'UserName',{
                  rules: [
                    { required: true, message: '', whitespace: true, trigger: 'change' },
                    { validator: validateUserName }
                  ]
                }
              ]"
              placeholder="请输入您的姓名"
            />
          </a-form-item>
          <a-form-item
            label="身份证号"
          >
            <a-input
              v-decorator="[
                'UserIdCard',{
                  rules: [
                    { required: true, message: '', whitespace: true, trigger: 'change' },
                    { validator: validateIDCard }
                  ]
                }
              ]"
              placeholder="请输入您的身份证号"
            />
          </a-form-item>
          <a-form-item
            label="联系电话"
          >
            <a-input
              v-decorator="[
                'UserPhone', {
                  rules: [
                    { required: true, message: '', whitespace: true, trigger: 'change' },
                    { validator: validatePhone }
                  ]
                }
              ]"
              placeholder="请输入您的联系电话"
            />
          </a-form-item>
          <a-form-item>
            <a-button type="primary" @click="handleSubmit">更新基本信息</a-button>
          </a-form-item>
        </a-form>

      </a-col>
      <a-col :order="1" :md="24" :lg="8" :style="{ minHeight: '180px' }">
        <div class="ant-upload-preview" @click="$refs.modal.edit(1)" >
          <a-icon type="cloud-upload-o" class="upload-icon"/>
          <div class="mask">
            <a-icon type="plus" />
          </div>
          <img :src="option.img"/>
        </div>
      </a-col>

    </a-row>

    <avatar-modal ref="modal" @ok="setavatar"/>

  </div>
</template>

<script>
// import debounce from 'lodash.debounce'
import AvatarModal from './AvatarModal'
import { baseMixin } from '@/store/app-mixin'
import { getUserInfo, setUsers } from '@/api/system'
import { validChineseChar, validIDCard, validTelphone } from '@/utils/validate'
import { mapGetters } from 'vuex'
export default {
  mixins: [baseMixin],
  components: {
    AvatarModal
  },
  data () {
    return {
      // cropper
      preview: {},
      option: {
        img: '/avatar2.jpg',
        info: true,
        size: 1,
        outputType: 'jpeg',
        canScale: false,
        autoCrop: true,
        // 只有自动截图开启 宽度高度才生效
        autoCropWidth: 180,
        autoCropHeight: 180,
        fixedBox: true,
        // 开启宽度和高度比例
        fixed: true,
        fixedNumber: [1, 1]
      },
      form: this.$form.createForm(this, { name: 'personal' })
    }
  },
  computed: {
    ...mapGetters(['userid'])
  },
  mounted () {
    this.fetchData()
  },
  methods: {
    fetchData () {
      getUserInfo({ userID: this.userid }).then(res => {
        const result = res.data
        this.$nextTick(() => {
           this.form.setFieldsValue({
            // LoginName: result.LoginName,
            UserName: result.UserName,
            UserIdCard: result.UserIdCard,
            UserPhone: result.UserPhone
          })
        })
      })
    },
    // validateLoginName (rule, value, callback) {
    //   if (!value) {
    //     callback(new Error('请输入您的登录账号!'))
    //   }
    //   if (!validAccountNumber(value)) {
    //     callback(new Error('请输入以字母开头的4-30位字母、数字或下划线的组合!'))
    //   }
    //   callback()
    // },
    validateUserName (rule, value, callback) {
      if (!value) {
        callback(new Error('请输入您的姓名!'))
      }
      if (!validChineseChar(value)) {
        callback(new Error('姓名输入有误，请重新输入!'))
      }
      callback()
    },
    validateIDCard (rule, value, callback) {
      if (!value) {
        callback(new Error('请输入您的身份证号!'))
      }
      if (!validIDCard(value)) {
        callback(new Error('身份证号输入有误，请重新输入!'))
      }
      callback()
    },
    validatePhone (rule, value, callback) {
      if (!value) {
        callback(new Error('请输入您的联系电话!'))
      }
      if (!validTelphone(value)) {
        callback(new Error('联系电话输入有误，请重新输入!'))
      }
      callback()
    },
    // handleChangeLoginName: debounce(function () {
    //   this.form.validateFields(['LoginName'], (errors, values) => {
    //     if (!errors) {
    //       const hide = this.$message.loading('正在核验账号是否重复...', 0)
    //       getUserName(values).then(res => {
    //         setTimeout(hide, 500)
    //         if (res.code !== 200) {
    //           setTimeout(() => {
    //             this.$message.error('您当前输入的登录账号重复，请检查后重新输入!', 3)
    //             this.form.setFieldsValue({ LoginName: '' })
    //           }, 1000)
    //         } else {
    //           this.$message.success('您当前输入的登录账号可用!')
    //         }
    //       }).catch((err) => {
    //         setTimeout(hide, 500)
    //         this.$message.error(err)
    //       })
    //     }
    //   })
    // }, 1000, { leading: false }),
    handleSubmit () {
      const that = this
      this.form.validateFields((err, values) => {
        if (!err) {
          this.$confirm({
            title: '请确认，是否更新您的基本信息?',
            content: '当您点击确认按钮时，系统将会自动更新您的基本信息，更新成功之后将会在1秒之后关闭当前对话框',
            onOk () {
              const { UserName, UserIdCard, UserPhone } = values
              setUsers({
                State: 'update',
                sys_users_Modules: {
                  UserID: '3',
                  UserHeadPhoto: '',
                  UserName,
                  UserIdCard,
                  UserPhone
                }
              }).then(res => {
                that.$message.success('信息更新成功! ')
              }).catch(() => {
                that.$message.error('信息更新失败! ')
              })
            }
          })
        }
      })
    },
    setavatar (url) {
      this.option.img = url
    }
  }
}
</script>

<style lang="less" scoped>

  .avatar-upload-wrapper {
    height: 200px;
    width: 100%;
  }

  .ant-upload-preview {
    position: relative;
    margin: 0 auto;
    width: 100%;
    max-width: 180px;
    border-radius: 50%;
    box-shadow: 0 0 4px #ccc;

    .upload-icon {
      position: absolute;
      top: 0;
      right: 10px;
      font-size: 1.4rem;
      padding: 0.5rem;
      background: rgba(222, 221, 221, 0.7);
      border-radius: 50%;
      border: 1px solid rgba(0, 0, 0, 0.2);
    }
    .mask {
      opacity: 0;
      position: absolute;
      background: rgba(0,0,0,0.4);
      cursor: pointer;
      transition: opacity 0.4s;

      &:hover {
        opacity: 1;
      }

      i {
        font-size: 2rem;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-left: -1rem;
        margin-top: -1rem;
        color: #d6d6d6;
      }
    }

    img, .mask {
      width: 100%;
      max-width: 180px;
      height: 100%;
      border-radius: 50%;
      overflow: hidden;
    }
  }
</style>
